Welcome back.
In this lecture we're going to continue to learn about data stores by understanding what an ordered
data store is.
Sometimes when we create games, we would like to create a universal leaderboard that shows like the
top ten players in a specific category, like top ten with most kills, top ten with the most money,
and so on.
And we can do so by storing player data within an ordered data store.
Now, if you remember inside of our leaderboard handler, we created our money data store by using the
Get Data Store function.
However, the data store that is returned by this function is not an ordered data store, meaning we
are unable to do things like grab the top ten players.
So we need to swap this out with an ordered data store instead of a regular data store.
To do this, there is another function within the data store service called Get Order Data Store.
So I'm going to create a new variable.
I'm going to call this one Ordered Money Data Store.
And that's going to be equal to the data store service get order data store.
And for this one we can just call it like player money.
However, note that this is going to be creating an entirely new data store, and any data store that
was stored in this previous data store will not show up in this ordered one.
So you need to make sure that you plan out what data you would like to store in an ordered data store
before you start scripting your game, because it will be very hard to migrate data later.
Now that we have this new ordered data store, I'm going to copy this and update the references within
my scripts.
So I don't want to get money data from our money data store anymore.
Instead, we're going to be using the Order Data Store.
And we're also going to be using the Order Data Store for saving money data.
And this ordered money data store has the exact same functions as a regular data store, such as update
async.
We also have set async, get async, and then it also has the standard function of remove async.
And I don't believe I talked about this function last time, but this one's very simple.
All you need to do is pass a key in here, and if it finds the key in the table, then it completely
removes that key and its value from the data store.
So that's what that function does.
But this ordered money data store has another function inside of it that we get to use.
And this function is called Get sorted async.
And it says it returns a data store pages object.
So what we do in this function is we specify if we would like to grab, um, data in ascending or descending
order.
In this case, since we want to grab the top ten players, then we need to keep ascending at false.
We can specify the page size or the number of keys that we would like to grab.
So if we would like to grab the top ten players then we would pass the number ten here.
And then there's some other optional data such as a minimum value stored at those keys, and a maximum
value.
So if any value stored does not match this minimum value or any value goes above the max value, then
it's going to ignore that key.
And it's not going to be included in this data store pages object.
So what I'm going to do is I'm going to create a new function.
And I'm going to call this function get top money players.
And I want this function to return to me a data store page that has the top five players.
So what we need to do is we need to refer to our ordered money data store and use the get sorted async
function.
We want to keep it descending.
So we're going to set this false for the ascending boolean.
And then we want to grab let's say the top five players with the most money.
Now since this is an async function, that means we also need to wrap it in a p call.
So we'll do p call, pass a function here, and then we'll control and x this and then return the value
returned from our ordered money data store will store this in the success and then the result variable.
And if we weren't successful in grabbing the top five players, then we'll just warn in the console.
Fail to grab top players.
And I'll just return.
Otherwise we can go ahead and get the top five players from the result.
And since this returns it says a data store pages object.
Let's go ahead and go over to the Roblox API to see what the functions are for a data store pages object.
So here in the Roblox documentation, it has a data store pages and it says a special type of pages
object whose pages contain key value pairs from an ordered data store.
For this object, the function get current page returns an array of tables, each containing keys named
key and value.
These reflect the key value pair data.
So it says right here.
And here's our example.
When we get our data store pages object, we have to use a function called get current page to get.
For example here they're getting a top ten.
And then they loop through this top ten.
It shows the rank or the index as well as the data stored at that rank.
So for example the number one player would have their data and that data would represent the key and
the value.
So the key would be the player's user ID.
And then the value would be how much money they have.
So we can go ahead and implement this into our script.
So once we get this result from our ordered money data store, the function is said to use is get current
page.
And this is going to return to us, our top five players.
And I'm going to go ahead and return these top five players out of our function.
And then what I'm going to do simply for demonstration purposes is I'm going to scroll down and before
our while loop, I'm just going to yield for five seconds.
And then we're going to go ahead and get the result from our Get Top Money players function.
And then we're going to go ahead and loop and get every single rank and data inside of our result.
And from here we can go ahead and print out into the console something like player, and then we can
pass the key.
Remember this data object or this table right here has a key called key.
So this would be our player's user ID.
So player with this user id we can say they are the rank or is rank.
And then we could pass the rank which would be this value right here.
And then we can say how much money they have.
So player with this user ID is rank let's say rank number one with how much money do they have.
And then we could pass the data dot value.
And then we could say they have this many dollars.
So five seconds after I joined the game, it should grab the top five players inside of our data store
and then it'll print into the console the top five players.
Since this is a newly created data store, there shouldn't be any data stored inside of here, meaning
this shouldn't print.
So what I need to do is I need to join the game.
I need to set some money for my player, leave the game and save that data and then rejoin again.
So let's go ahead and play test the game.
And since we're using that new data store, of course I have zero money, but I'm going to go ahead
and update the value inside of here.
We could set it to something like 1000.
And then I can go ahead and leave the game to save that data.
And now I can go back and play my game again.
Here we have my $1,000 and it should print into the console.
There we go.
Player there is.
My user ID is ranked number one with $1,000.
And just like that, we have successfully grabbed the top players in our game with the most money.
Now you can do a lot with this data.
You could update a surface UI that shows all the top players in your game with the most money.
There's a whole bunch of different stuff you can do with it.
It's really up to your own creative preference.
Otherwise that's how simple it is to use ordered data stores.
Not much more than that.
Keep up the good work and I'll see you in the next one.